package com.manager.commons.utils;

import com.manager.commons.log.LogFactory;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class RequestUtil {
    protected Logger logger = LogFactory.getLogger(this.getClass());

    private RequestUtil() {
    }


    /**
     * request的全部参数转换成map
     *
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    public Map<String, String[]> getMap(HttpServletRequest request) {

        return request.getParameterMap();
    }






    /**
     * 将map转换成bean对象：
     * 根据Map的keyValues创建Bean对象
     *
     * @param theClass
     * @param keyValues
     * @return
     */
    public <T> T getBean(Class<T> theClass, Map<String, Object> keyValues) {
        T bean = null;
        try {
            bean = theClass.newInstance();
            BeanUtils.populate(bean, keyValues);
        } catch (Exception e) {
            logger.error("map转换成bean对象出错", e);
        }
        return bean;
    }
}
